/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.utils;

import com.google.common.collect.Lists;
import com.intelligent.ispc.core.dto.KeyValueDto;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.List;

/**
 * Created by cherry on 2016/8/25.
 */
public class FileUtils {
    private static JFileChooser fileChooser;
    public static final int FILES_ONLY = JFileChooser.FILES_ONLY;
    public static final int FILES_AND_DIRECTORIES = JFileChooser.FILES_AND_DIRECTORIES;
    public static final int DIRECTORIES_ONLY = JFileChooser.DIRECTORIES_ONLY;

    /**
     * Find all files demands regType of directory.
     *
     * @param currentPath file absolute path
     * @param regType     file format reg
     * @return list of key value dto.
     */
    public static List<KeyValueDto> findFiles(String currentPath, String regType) {
        List<KeyValueDto> dtos = Lists.newArrayList();

        File file = new File(currentPath);
        File[] filelist = file.listFiles();
        if (filelist != null) {
            for (int i = 0; i < filelist.length; i++) {
                String name = filelist[i].getName();
                if (filelist[i].isDirectory()) {
                    dtos.addAll(findFiles(filelist[i].getAbsolutePath(), regType));
                } else {
                    KeyValueDto dto = new KeyValueDto();
                    String[] values = name.split("\\.");
                    if ((values[values.length - 1]).matches(regType)) {
                        dto.setKey(name);
                        dto.setValue(filelist[i].getAbsolutePath());
                        dtos.add(dto);
                    }
                }
            }
        }
        return dtos;
    }

    /**
     * Choice file with
     *
     * @param parent  parent component, such as dialog.
     * @param dtos    file name and path mapping.
     * @param regType extension of file
     * @return array of file
     */
    public static String[] fileSelect(Component parent, List<KeyValueDto> dtos, String regType) {
        JFileChooser jfc = FileUtils.getFileChooser();
        jfc.setFileSelectionMode(FILES_AND_DIRECTORIES);
        jfc.setMultiSelectionEnabled(true);
        int index = jfc.showOpenDialog(parent);
        File[] files = jfc.getSelectedFiles();
        String selectedFiles = "";
        String existFiles = "";

        if (index == JFileChooser.FILES_ONLY) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                selectedFiles += file.getName() + System.getProperty("line.separator");
                if (file.isDirectory()) {
                    List<KeyValueDto> fileDtos = findFiles(file.getAbsolutePath(), regType);
                    for (KeyValueDto dto : fileDtos) {
                        boolean hasExistFile = false;
                        for (KeyValueDto tdto : dtos) {
                            if (tdto.getKey().equals(dto.getKey())) {
                                existFiles += dto.getKey() + System.getProperty("line.separator");
                                hasExistFile = true;
                                break;
                            }
                        }
                        if (hasExistFile) {
                            continue;
                        }
                        dtos.add(dto);
                    }
                } else if (file.isFile()) {
                    KeyValueDto dto = new KeyValueDto();
                    String[] values = file.getName().split("\\.");
                    if ((values[values.length - 1]).matches(regType)) {
                        boolean hasExistFile = false;
                        for (KeyValueDto tdto : dtos) {
                            if (tdto.getKey().equals(file.getName())) {
                                existFiles += file.getName() + System.getProperty("line.separator");
                                hasExistFile = true;
                                break;
                            }
                        }
                        if (hasExistFile) {
                            continue;
                        }
                        dto.setKey(file.getName());
                        dto.setValue(file.getAbsolutePath());
                        dtos.add(dto);
                    }
                }
            }
        }
        return new String[]{selectedFiles, existFiles};
    }

    /**
     * Return file path.
     *
     * @param parent parent component.
     * @return file path.
     */
    public static String filePathSelect(Component parent) {
        return filePathSelect(parent, FILES_AND_DIRECTORIES);
    }

    /**
     * Return file path with modal.
     *
     * @param parent parent component.
     * @param modal  selection modal. FILES_ONLY, FILES_AND_DIRECTORIES, DIRECTORIES_ONLY
     * @return selected files
     */
    public static String filePathSelect(Component parent, int modal) {
        JFileChooser jfc = FileUtils.getFileChooser();
        jfc.setFileSelectionMode(modal);
        jfc.setMultiSelectionEnabled(true);
        int index = jfc.showOpenDialog(parent);
        File[] files = jfc.getSelectedFiles();
        String selectedFiles = "";

        if (index == JFileChooser.FILES_ONLY) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                selectedFiles += file.getPath();
            }
        }
        return selectedFiles;
    }

    /**
     * Get a file chooser instance.
     *
     * @return instance.
     */
    public static JFileChooser getFileChooser() {
        if (fileChooser == null) {
            fileChooser = new JFileChooser();
        }
        return fileChooser;
    }
}
